/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-06-28 19:34:30
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:39:18
 */
import { Component, Injector, ViewChild } from '@angular/core';
import { Table } from 'primeng/components/table/table';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { UserCodeEditOrAddComponent } from "./user-code-edit-or-add/user-code-edit-or-add.component";
import { MessageService } from '@abp/message/message.service';
import { SysCodeServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
@Component({
    selector: 'app-user-code',
    templateUrl: './user-code.component.html',
    styleUrls: ['./user-code.component.less']
})
export class UserCodeComponent extends AppComponentBase {
    @ViewChild('typePaginator') typePaginator: Paginator;
    @ViewChild('typeData') typeData: Table;
    @ViewChild('sonPaginator') sonPaginator: Paginator;
    @ViewChild('sonData') sonData: Table;
    @ViewChild('userCodeEditOrAdd') userCodeEditOrAdd: UserCodeEditOrAddComponent;
    rowdata: any;//单行数据
    son: any;//修改数据
    title = '';// 子组件标题
    selected: any;// 左侧选择行数据
    id: any;
    pttr = '00000000-0000-0000-0000-000000000000';
    saveFlag = true;
    // isUseds = [];//是否使用下拉内容
    // isUsed = undefined;// 下拉选择内容
    // name= undefined; // 搜索条件名称
    // function= undefined; // 搜索条件功能模块
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _sysCodeServiceProxy: SysCodeServiceProxy,) {
        // this.isUseds = [
        //   {name:'是', code: 1},
        //   {name:'否', code: 0}
        //   ]
        super(injector);

    }

    ngOnInit() {
    }
    // serach() {

    // }
    // resetSearch() {
    //     this.isUsed = undefined;
    //     this.name = undefined;
    //     this.function = undefined;
    // }
    getTypeTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.typePaginator.changePage(0);
            return;
        }
        this._sysCodeServiceProxy.getCodeListByParentId(
            undefined,
            undefined,
            undefined,
            this.pttr,
            undefined,
            undefined,
            this.primengTableHelper.getSorting(this.typeData),
            this.primengTableHelper.getMaxResultCount(this.typePaginator, event),
            this.primengTableHelper.getSkipCount(this.typePaginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
        // this.primengTableHelper.records = [{ name: 'test', id: '1',show:'测试'},{ name: 'test2', id: '2',show:'测试2'}];
    }
    // 左侧表格点击事件
    searchSon(row) {
        this.id = row.id;
        this.rowdata = row;
        this.getSonTableList();
    }
    getSonTableList(event?: LazyLoadEvent) {
        this._sysCodeServiceProxy.getCodeListByParentId(
            undefined,
            undefined,
            undefined,
            this.rowdata.id,
            undefined,
            undefined,
            this.primengSonTableHelper.getSorting(this.sonData),
            this.primengSonTableHelper.getMaxResultCount(this.sonPaginator, event),
            this.primengSonTableHelper.getSkipCount(this.sonPaginator, event)
        ).pipe(finalize(() => this.primengSonTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengSonTableHelper.totalRecordsCount = result.totalCount;
            this.primengSonTableHelper.records = result.items;
            this.primengSonTableHelper.hideLoadingIndicator();
        });
    }
    addSon() {
        if (this.selected == null) {
            this._messageService.error('请先选择类别！');
        } else {
            this.title = '新增';
            setTimeout(() => {
                this.userCodeEditOrAdd.show();
            }, 10)
        }

    }
    update(row) {
        setTimeout(() => {
            this.title = '修改';
            this.son = row;
            this.userCodeEditOrAdd.show();
        }, 10)
    }
    delete(row) {
        this.message.confirm(
            '确认删除这条数据吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    if (this.saveFlag) {
                        this.saveFlag = false;
                        this._sysCodeServiceProxy.deleteCodeById(
                            row.id,
                        ).pipe(finalize(() => this.primengSonTableHelper.hideLoadingIndicator())).subscribe(result => {
                            this.saveFlag = true;
                            if (result.code == 0) {
                                this._messageService.success('删除成功！');
                                this.getSonTableList();
                            } else {
                                this._messageService.error('删除失败！');
                            }
                        });
                    }
                }
            }
        );
    }
}
